//显示权限的树形结构
function fillAuthTree() {
    var ajaxReturn = $.ajax({
        "url": "assign/get/all/auth",
        "type": "post",
        "dataType": "json",
        "async": false
    });
    if (ajaxReturn.status != 200) {
        layer.msg("请求处理出错,响应状态码是：" + ajaxReturn.status + " 说明是:" + ajaxReturn.statusText);
        return;
    }
    var authList = ajaxReturn.responseJSON.queryData;
    var setting = {
        "data": {
            "simpleData": {
                "enable": true,
                "pIdKey": "categoryId"
            },
            "key": {
                "name": "title"
            }
        },
        "check": {
            "enable": true
        }
    };
    //生成树形结构
    $.fn.zTree.init($("#authTreeDemo"), setting, authList);
    //获取zTree对象
    var zTreeObj = $.fn.zTree.getZTreeObj("authTreeDemo");
    zTreeObj.expandAll(true);
    //获取该角色已分配的权限
    var ajaxResultById = $.ajax({
        "url": "assign/get/auth/by/id",
        "type": "post",
        "data": {
            "roleId": window.roleId
        },
        "dataType": "json",
        "async": false
    });
    if (ajaxResultById.status != 200) {
        layer.msg("请求处理出错,响应状态码是：" + ajaxResultById.status + " 说明是:" + ajaxResultById.statusText);
        return;
    }
    var authIdArray = ajaxResultById.responseJSON.queryData;

    for (var i = 0; i < authIdArray.length; i++) {
        var authId = authIdArray[i];
        var treeNode = zTreeObj.getNodeByParam("id", authId);

        //第二个参数表示勾选，第三个参数表示不联动勾选
        zTreeObj.checkNode(treeNode, true, false);
    }

}

//删除时打开模态框
function showRemoveModal(roleArray) {

    $("#removeModal").modal("show");

    $("#removeSpan").empty();

    window.roleIdArray = [];

    for (var i = 0; i < roleArray.length; i++) {
        var roleName = roleArray[i].roleName;
        $("#removeSpan").append(roleName + " ");
        var roleId = roleArray[i].roleId;
        window.roleIdArray.push(roleId);
    }
}

//执行分页操作
function generatePage() {
    var pageInfo = getPageInfoRemote();
    fullTableBody(pageInfo);
}

// 远程访问服务器端程序获取pageInfo数据
function getPageInfoRemote() {
    var ajaxResult = $.ajax({
        "url": "role/get/page",
        "type": "post",
        "data": {
            "pageNum": pageNum,
            "pageSize": pageSize,
            "keyword": keyword
        },
        "async": false,
        "datatype": "json"
    });
    var status = ajaxResult.status;
    if (status != 200) {
        layer.msg("请求失败！状态码：" + status + "失败信息：" + ajaxResult.statusText)
    }
    let resultEntity = ajaxResult.responseJSON;
    let result = resultEntity.operationResult;
    if (result == "FAILED") {
        layer.msg(resultEntity.message);
        return null;
    }
    let pageInfo = resultEntity.queryData;
    return pageInfo;
}

//填充表格
function fullTableBody(pageInfo) {
    $("#pageBody").empty();
    $("#Pagination").empty();
    if (pageInfo == null || pageInfo == undefined || pageInfo.list == null || pageInfo.list.length == 0) {
        $("#pageBody").append("<tr colspan='4'>抱歉！没有您要的数据！</tr>");
        return;
    }
    for (let i = 0; i < pageInfo.list.length; i++) {
        var role = pageInfo.list[i];
        var roleId = role.id;
        var roleName = role.name;

        var roleIdTd = "<td>" + roleId + "</td>";
        var roleCheckboxTd = "<td><input id=" + roleId + " class='itemBtn' type='checkbox'></td>";
        var roleNameTd = "<td>" + roleName + "</td>";

        var btn1 = "<button id=" + roleId + " type='button' class='btn btn-success btn-xs checkBtn'><i class='glyphicon glyphicon-check'></i></button>";
        var btn2 = "<button id=" + roleId + " type='button' class='btn btn-primary btn-xs pencilBtn'><i class='glyphicon glyphicon-pencil'></i></button>";
        var btn3 = "<button id=" + roleId + " type='button' class='btn btn-danger btn-xs removeBtn'><i class='glyphicon glyphicon-remove'></i></button>";

        var btnTd = "<td>" + btn1 + " " + btn2 + " " + btn3 + "</td>";

        var tr = "<tr>" + roleIdTd + roleCheckboxTd + roleNameTd + btnTd + "</tr>";

        $("#pageBody").append(tr);
    }
    //生成页面导航条
    generateNavigator(pageInfo);
}

//生成页面导航条
function generateNavigator(pageInfo) {
    // 获取总记录数
    var totalRecord = pageInfo.total;
    // 声明相关属性
    var properties = {
        "num_edge_entries": 3, // 边缘页数
        "num_display_entries": 5, // 主体页数
        "callback": paginationCallBack,
        "items_per_page": pageInfo.pageSize, // 每页显示5项
        "current_page": pageInfo.pageNum - 1, // Pagination内部使用pageIndex来管理页码，从0开始，而pageNum从1开始
        "prev_text": "上一页",
        "next_text": "下一页"
    };
    // 调用pagination()函数
    $("#Pagination").pagination(totalRecord, properties);
}

// 翻页时的回调函数
function paginationCallBack(pageIndex, jQuery) {
    // 根据pageIndex计算得到pageNum
    window.pageNum = pageIndex + 1;
    // 调用分页函数
    generatePage();
    // 由于每一个页码按钮都是超链接，所以在这个函数最后取消超链接的默认行为
    return false;
}